<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-toggle-button/paper-toggle-button.html">

<link rel="import" href="../tf-regex-group/tf-regex-group.html">
<link rel="import" href="../tf-dashboard-common/tensorboard-color.html">

<!--
`tf-categorizer` turns an array of tags into an array of categories

The transformation from tags to categories is controlled by the user, through
interacting with the categorizer widget.

(See type signatures in categorizer.ts)

Example:
  <tf-categorizer tags="[[tags]]" categories="{{categories}}"></tf-categorizer>

Public Properties:
`tags` - Array of strings that are the tags to categorize. Should be one-way bound downward.
`categories` - Array of Categorizer.Category objects that are generated by the Categorizer.
  Are readOnly and notify: True. Expected to be one-way bound upward.

The categorizer provides inputs for adding regular expression rules and toggling whether
categories are exclusive.
-->
<dom-module id="tf-categorizer">
  <template>
    <div class="inputs">
      <tf-regex-group id="regex-group" regexes="{{regexes}}"></tf-regex-group>
    </div>
    <div id="underscore-categorization">
      <paper-checkbox
        checked="{{splitOnUnderscore}}"
      >Split on underscores</paper-checkbox>
    </div>
    <style>
      :host {
        display: block;
        padding-bottom: 15px;
      }
      paper-checkbox {
        --paper-checkbox-checked-color: var(--paper-grey-600);
        --paper-checkbox-unchecked-color: var(--paper-grey-600);
        font-size: 14px;
      }
      #underscore-categorization {
        color: var(--paper-grey-700);
      }
    </style>
  </template>
  <script src="categorizer.js"></script>
  <script>
    Polymer({
      is: "tf-categorizer",
      properties: {
        regexes: {type: Array},
        tags: {type: Array},
        categoriesAreExclusive: {type: Boolean, value: true},
        fallbackCategorizer: {
          type: String,
          computed: "chooseFallbackCategorizer(splitOnUnderscore)"
        },
        splitOnUnderscore: {
          type: Boolean,
          value: false,
        },
        categorizer: {
          type: Object,
          computed: "computeCategorization(regexes.*, categoriesAreExclusive, fallbackCategorizer)",
        },
        categories: {type: Array, value: function() {return [];}, notify: true, readOnly: true},
      },
      observers: ['recategorize(tags.*, categorizer)'],
      computeCategorization: function(regexes, categoriesAreExclusive, fallbackCategorizer) {
        var categorizationStrategy = {
          categoryDefinitions: regexes.base,
          categoriesAreExclusive: categoriesAreExclusive,
          fallbackCategorizer: fallbackCategorizer,
        };
        return Categorizer.categorizer(categorizationStrategy);
      },
      recategorize: function() {
        this.debounce("tf-categorizer-recategorize", function (){
          var categories = this.categorizer(this.tags);
          this._setCategories(categories);
        })
      },
      chooseFallbackCategorizer: function(splitOnUnderscore) {
        if (splitOnUnderscore) {
          return "LegacyUnderscoreCategorizer";
        } else {
          return "TopLevelNamespaceCategorizer";
        }
      },
    });
  </script>
</dom-module>
